<?php
/*********************************************************************************
** The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
*
 ********************************************************************************/
?>
	<div class="left">
      <input align="left" class="crmbutton small cancel"type="button" value="<?php echo getTranslatedString('LBL_BACK_BUTTON');?>" onclick="window.history.back();"/>
	</div>
	<div class="right">
      <input class="crmbutton small cancel" name="newticket" type="submit" value="<?php echo getTranslatedString('LBL_NEW_TICKET');?>" onclick="this.form.module.value='HelpDesk';this.form.action.value='index';this.form.fun.value='newticket'">
    	<input class="crmbutton small cancel" name="srch" type="button" value="<?php echo getTranslatedString('LBL_SEARCH');?>" onClick="showSearchFormNow('tabSrch');">
 	</div>
</form>
</div><!-- End action -->
<?php

global $result;
global $client;
global $Server_Path;

$customerid = $_SESSION['customer_id'];
$sessionid = $_SESSION['customer_sessionid'];

echo '<table class="list">' . "\n";

if($ticketid != '')
{
	
	$params = array('id' => "$ticketid", 'block'=>"$block",'contactid'=>$customerid,'sessionid'=>"$sessionid");
	$result = $client->call('get_details', $params, $Server_Path, $Server_Path);	
	// Check for Authorization
	if (count($result) == 1 && $result[0] == "#NOT AUTHORIZED#") {
		echo '<tr><td class="error">' . getTranslatedString('LBL_NOT_AUTHORISED') . '</td></tr>' ."\n";
		echo '</table>' . "\n";
		include("bottom.php");
		die();
	}
	$ticketinfo = $result[0][$block];
	$params = Array(Array('id'=>"$customerid", 'sessionid'=>"$sessionid", 'ticketid' => "$ticketid"));
	$commentresult = $client->call('get_ticket_comments', $params, $Server_Path, $Server_Path);
	$ticketscount = count($result);
	$commentscount = count($commentresult);
	$params = Array(Array('id'=>"$customerid", 'sessionid'=>"$sessionid", 'ticketid' => "$ticketid"));
		
	//Get the creator of this ticket
	$creator = $client->call('get_ticket_creator', $params, $Server_Path, $Server_Path);

	$ticket_status = '';
	foreach($ticketinfo as $key=>$value) {
		$fieldlabel = $value['fieldlabel'];
		$fieldvalue = $value['fieldvalue'];
		if ($fieldlabel == 'Status') {
			$ticket_status = $fieldvalue;
			break;
		}
	}
	//If the ticket is created by this customer and status is not Closed then allow him to Close this ticket otherwise not
	if ($ticket_status != 'Closed' && $ticket_status != '') {
		$ticket_close_link = '<a href=index.php?module=HelpDesk&amp;action=index&amp;fun=close_ticket&amp;ticketid=' . $ticketid . '>' . getTranslatedString('LBL_CLOSE_TICKET') . '</a>';
	} else {
		$ticket_close_link = '';
	}
   
   echo getblock_header('Ticket Information','4',$ticket_close_link);	
	echo getblock_fieldlist($ticketinfo);
	
	if($commentscount >= 1 && is_array($commentresult))
	{
	
		$list .= '
		   <tr>' . "\n" . '<th colspan="5" class="detailedViewHeader">' . getTranslatedString('LBL_TICKET_COMMENTS') . '</th>' . "\n" . '</tr>
		   <tr>
			<td colspan="4" class="dvtCellInfo">
			   <div id="scrollTab2">
				<table width="100%"  border="0" cellspacing="5" cellpadding="5">';

		//Form the comments in between tr tags
		for($j=0;$j<$commentscount;$j++)
		{
			$list .= '
				   <tr>
					<td width="5%" valign="top">'.($commentscount-$j).'</td>
					<td width="95%">'.$commentresult[$j]['comments'].'<br><span class="hdr">'.getTranslatedString('LBL_COMMENT_BY').' : '.$commentresult[$j]['owner'].' '.getTranslatedString('LBL_ON').' '.$commentresult[$j]['createdtime'].'</span></td>
				   </tr>';
		}

		$list .= '
				</table>
			   </div>
			</td>
		   </tr>';
	}
	
	
	if($ticket_status != 'Closed')
	{
		$list .= '
		   <tr>' . "\n" . '<th colspan="4" class="detailedViewHeader">' . getTranslatedString('LBL_ADD_COMMENT') . '</th>' . "\n" . '</tr>' . "\n" . '
		   <form name="comments" action="index.php" method="post">
		   <input type="hidden" name="module">
		   <input type="hidden" name="action">
		   <input type="hidden" name="fun">
		   <input type=hidden name=ticketid value='.$ticketid.'>
		   <tr>
			<td colspan="4" class="dvtCellInfo"><textarea name="comments" cols="55" rows="5" class="detailedViewTextBox"  onFocus="this.className=\'detailedViewTextBoxOn\'" onBlur="this.className=\'detailedViewTextBox\'"></textarea></td>
		   </tr>
		   <tr>
			    <td><input class="crmbutton small cancel" title="' . getTranslatedString('LBL_SUBMIT') . '" accesskey="S" class="small"  name="submit" value="' . getTranslatedString('LBL_SUBMIT') . '" style="width: 70px;" type="submit" onclick="this.form.module.value=\'HelpDesk\';this.form.action.value=\'index\';this.form.fun.value=\'updatecomment\'; if(trim(this.form.comments.value) != \'\')	return true; else return false;"/></td>
		   </form>
			    <td colspan="2">&nbsp;</td>
			    <td>&nbsp;</td>
		   </tr>';
	}
	$files_array = getTicketAttachmentsList($ticketid);
	if($files_array[0] != "#MODULE INACTIVE#"){
		$list .= '
	
			   <!--  Added for Attachment -->
			   <tr><td colspan=4>&nbsp;</td></tr>
			   <tr><th colspan="4" class="detailedViewHeader">' . getTranslatedString('LBL_ATTACHMENTS') . '</th></tr>';
	
		//Get the attachments list and form in the tr tag
		$attachments_count = count($files_array);
		if(is_array($files_array))
		{
			for($j=0;$j<$attachments_count;$j++)
			{
				$filename = $files_array[$j]['filename'];
				$filetype = $files_array[$j]['filetype'];
				$filesize = $files_array[$j]['filesize'];
				$fileid = $files_array[$j]['fileid'];
				$filelocationtype = $files_array[$j]['filelocationtype'];
				//To display the attachments title
				$attachments_title = '';
				if($j == 0)
					$attachments_title = '' . getTranslatedString('LBL_ATTACHMENTS') . '';
				if($filelocationtype == 'I'){
					$list .= '
					   		<tr>
								   <td class="dvtCellLabel">' . $attachments_title . '</td>
								   <td class="dvtCellInfo" colspan="3"><a href="index.php?downloadfile=true&amp;fileid=' . $fileid . '&amp;filename=' . $filename . '&amp;filetype=' . $filetype . '&amp;filesize=' . $filesize . '&amp;ticketid=' . $ticketid . '">' . ltrim($filename,$ticketid.'_') . '</a></td>
					   		</tr>' . "\n";
				}else {
					$list .='<tr>
								   <td class ="dvtCellLabel">' . $attachments_title . '</td>
								   <td class="dvtCellInfo" colspan="3"><a target="_blank" href=' . $filename . '>' . $filename . '</a></td>
							   </tr>' . "\n";
				}
			}
		}
		else{
			$list .= '<tr>
			             <td>' . getTranslatedString('NO_ATTACHMENTS') . '</td>
			          </tr>' . "\n";
		}
	}
	//To display the file upload error
	if($upload_status != '')
	{
		$list .= '<tr>
				<td class="dvtCellLabel" align="right">' . getTranslatedString('LBL_FILE_UPLOADERROR') . '</td>
				<td class="dvtCellInfo" colspan="3"><span class="error">' . $upload_status . '</span></td>
			   </tr>' . "\n";
		
	}

	//Provide the Add Comment option if the ticket is not Closed
	if($ticket_status != 'Closed' && $files_array[0] != "#MODULE INACTIVE#")
	{
		//To display the File Browse option to upload attachment	
		$list .= '
		   <tr>
			<form name="fileattachment" method="post" enctype="multipart/form-data" action="index.php">
   			<input type="hidden" name="module" value="HelpDesk">
   			<input type="hidden" name="action" value="index">
   			<input type="hidden" name="fun" value="uploadfile">
   			<input type="hidden" name="ticketid" value="' . $ticketid . '">
				<td class="dvtCellLabel">' . getTranslatedString('LBL_ATTACH_FILE') . '</td>
				<td colspan=2 class="dvtCellInfo"><input type="file" size="50" name="customerfile" class="detailedViewTextBox" onchange="validateFilename(this)" />
				<input type="hidden" name="customerfile_hidden"/></td> 
				<td id="attach_btn"><input class="crmbutton small cancel" name="Attach" type="submit" value="' . getTranslatedString('LBL_ATTACH') . '"></td>
			</form>
		   </tr>' . "\n";
	}
	
	echo $list;

} else {
	echo getTranslatedString('LBL_NONE_SUBMITTED');
}

echo '</table>' . "\n";

$filevalidation_script = <<<JSFILEVALIDATION
   <script type="text/javascript">
                   
   function getFileNameOnly(filename) {
   	var onlyfilename = filename;
     	// Normalize the path (to make sure we use the same path separator)
    	var filename_normalized = filename.replace(/\\\\/g, '/');
     	if(filename_normalized.lastIndexOf("/") != -1) {
       	onlyfilename = filename_normalized.substring(filename_normalized.lastIndexOf("/") + 1);
     	}
     	return onlyfilename;
   }
   
   /* Function to validate the filename */
   function validateFilename(form_ele) {
   if (form_ele.value == '') return true;
   	var value = getFileNameOnly(form_ele.value);
   	// Color highlighting logic
   	var err_bg_color = "#FFAA22";
   	if (typeof(form_ele.bgcolor) == "undefined") {
   		form_ele.bgcolor = form_ele.style.backgroundColor;
   	}
   	// Validation starts here
   	var valid = true;
   	/* Filename length is constrained to 255 at database level */
   	if (value.length > 255) {
   		alert(alert_arr.LBL_FILENAME_LENGTH_EXCEED_ERR);
   		valid = false;
   	}
   	if (!valid) {
   		form_ele.style.backgroundColor = err_bg_color;
   		return false;
   	}
   	form_ele.style.backgroundColor = form_ele.bgcolor;
   	form_ele.form[form_ele.name + '_hidden'].value = value;
   	return true;
   }
   </script>
JSFILEVALIDATION;

if ($ticket_status != 'Closed') {
   echo $filevalidation_script;
}
?>